package com.cleargroup.magic.cleara.c.command;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;


public class TelephonyUtil {
	private static final String TAG = TelephonyUtil.class.getSimpleName();

	public static TelephonyManager TM(Context context) {
		return (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
	}

	public static String getResolution(Activity activity) {
		return getWidthPixels(activity) + " x " + getHeightPixels(activity);
	}

	public static int getWidthPixels(Activity activity) {
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	public static int getHeightPixels(Activity activity) {
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.heightPixels;
	}

	public static boolean isCallStateIdle(Context context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getCallState() == TelephonyManager.CALL_STATE_IDLE;
	}

	public static int getScreenBrightnessPercent(Activity activity) {
		return getScreenBrightness(activity) * 100 / 255;
	}

	public static int getScreenBrightness(Activity activity) {
		int value = 0;
		ContentResolver cr = activity.getContentResolver();
		try {
			value = Settings.System.getInt(cr,
					Settings.System.SCREEN_BRIGHTNESS);
		} catch (Exception e) {
		}
		return value;
	}

	public static String getOperatorName(Context context) {
		if (TM(context).getNetworkOperatorName() != null
				&& !(TM(context).getNetworkOperatorName().equals(""))) {
			return TM(context).getNetworkOperatorName();
		} else {
			return "未知";
		}
	}

	/**
	 *SIM_STATE_UNKNOWN
	 *SIM_STATE_ABSENT
	 *SIM_STATE_PIN_REQUIRED
	 *SIM_STATE_PUK_REQUIRED
	 *SIM_STATE_NETWORK_LOCKED
	 *SIM_STATE_READY
	 */
	public static int getSimState(Context context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getSimState();
	}


}
